	package oppsa.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import oppsa.controller.Column;
import oppsa.controller.ColumnManager;
import oppsa.controller.Translate;

import org.jdesktop.swingx.JXFrame;

public class UpdateColBox extends JXFrame {
	private JTextField textFieldColumnName;
	private JPanel panel;
	private JLabel lblAddUser;
	private JLabel lblUserChoice;
	private JButton btnSubmit;
	private JButton source;
	private JButton btnCancel;
	private ProjectDetailsView pdv;
	private ProjectView pv;
	private Column c;
	private ColumnManager cm;

	public UpdateColBox(ProjectDetailsView pdv ,boolean show) {
		setIconImage(Toolkit.getDefaultToolkit().getImage(UpdateColBox.class.getResource("/logo.png")));
		this.pdv = pdv;
		cm = pdv.getCm();
		if (show) {
			c = pdv.getCm_fieldsShown().getColumnListe().get(pdv.getSelectedIndex());
		} else {
			c = pdv.getCm_fieldsHidden().getColumnListe().get(pdv.getSelectedIndex());
		}
		final Translate t = new Translate();

		pdv.addWindowFocusListener(clickhomeview);
		setStartPosition(StartPosition.CenterInScreen);
		getRootPaneExt().getContentPane().setBackground(Color.RED);
		setMinimumSize(new Dimension(400, 200));
		setPreferredSize(new Dimension(400, 200));
		setResizable(false);
		
		setTitle(t.getLocalizedText("tlRenameField"));

		panel = new JPanel();
		panel.setBackground(Color.WHITE);
		getRootPaneExt().getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new MigLayout("", "[][grow]", "[][grow][][][][grow]"));

		// oliver
		lblAddUser = new JLabel(t.getLocalizedText("lblRenameField"));
		lblAddUser.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel.add(lblAddUser, "cell 0 0");

		// oliver
		lblUserChoice = new JLabel(t.getLocalizedText("lblRename"));
		lblUserChoice.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel.add(lblUserChoice, "cell 0 2,alignx left");

		textFieldColumnName = new JTextField(); 

		textFieldColumnName.setText(c.getName());
		textFieldColumnName.setPreferredSize(new Dimension(110, 22));
		panel.add(textFieldColumnName, "cell 1 2,alignx left");

		btnSubmit = new JButton("");
		btnSubmit.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
		btnSubmit.setBorder(null);
		btnSubmit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				int i = cm.findIndex(c);
				c.setName(textFieldColumnName.getText());
				cm.getColumnListe().set(i, c);
				cm.Save();
				cm = new ColumnManager(true);
				setVisible(false);
				UpdateColBox.this.pdv.UpdateList();
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("SubmitS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
			}
		});
		panel.add(btnSubmit, "flowx,cell 1 4");

		btnCancel = new JButton("");
		btnCancel.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
		btnCancel.setBorder(null);
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				setVisible(false);
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("CancelS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("CancelS"))));
			}
		});
		panel.add(btnCancel, "cell 1 4");
	}
	WindowFocusListener  clickhomeview = new  WindowFocusListener () {

		@Override
		public void windowGainedFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			toFront();
		}

		@Override
		public void windowLostFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}



	};

	
}
/*
	public UpdateColBox(ProjectView pv ,boolean show) {
		setIconImage(Toolkit.getDefaultToolkit().getImage(UpdateColBox.class.getResource("/logo.png")));
		this.pv = pv;
		cm = pv.getCm();
		final Translate t = new Translate();

		setAlwaysOnTop(true);
		setStartPosition(StartPosition.CenterInScreen);
		getRootPaneExt().getContentPane().setBackground(Color.RED);
		setMinimumSize(new Dimension(400, 200));
		setPreferredSize(new Dimension(400, 200));
		setResizable(false);
		
		setTitle(t.getLocalizedText("tlRenameField"));

		panel = new JPanel();
		panel.setBackground(Color.WHITE);
		getRootPaneExt().getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new MigLayout("", "[][grow]", "[][grow][][][][grow]"));

		// oliver
		lblAddUser = new JLabel(t.getLocalizedText("lblRenameField"));
		lblAddUser.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel.add(lblAddUser, "cell 0 0");

		// oliver
		lblUserChoice = new JLabel(t.getLocalizedText("lblRename"));
		lblUserChoice.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel.add(lblUserChoice, "cell 0 2,alignx left");

		textFieldColumnName = new JTextField();

		textFieldColumnName.setText(c.getName());
		textFieldColumnName.setPreferredSize(new Dimension(110, 22));
		panel.add(textFieldColumnName, "cell 1 2,alignx left");

		btnSubmit = new JButton("");
		btnSubmit.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
		btnSubmit.setBorder(null);
		btnSubmit.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent arg0) {
				int i = cm.findIndex(c);
				c.setName(textFieldColumnName.getText());
				cm.getColumnListe().set(i, c);
				cm.Save();
				cm = new ColumnManager(true);
				setVisible(false);
				UpdateColBox.this.pv.UpdateList();
			}

			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("SubmitS"))));
			}

			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
			}
		});
		panel.add(btnSubmit, "flowx,cell 1 4");

		btnCancel = new JButton("");
		btnCancel.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
		btnCancel.setBorder(null);
		btnCancel.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent e) {
				setVisible(false);
			}

			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("CancelS"))));
			}

			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("CancelS"))));
			}
		});
		panel.add(btnCancel, "cell 1 4");
	}

	private int getKeyForValue(String value, TreeMap<Integer, String> map) {
		for (Entry<Integer, String> entry : map.entrySet()) {
			if (entry.getValue().equals(value)) {
				return entry.getKey();
			}
		}
		return -1;
	}
}*/
